# -*- coding: utf-8 -*-
# $Id: statwindow.py 27 2009-02-15 19:46:27Z gkuznets $

# Copyright 2008, 2009 Grigoriy Kuznetsov (gkuznets@yandex.ru)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from PyQt4.QtGui import QWidget, QHBoxLayout, QVBoxLayout, QGridLayout, QLabel
from PyQt4.QtCore import QTimer, SIGNAL, QString
from core.batch import Batch
from core.group import Group
from checkup import Checkup
chekup = Checkup.instance()
from stat.twoprogressbar import TwoProgressBar
from stat.problemstatwidget import ProblemStatWidget
from stat.overalltable import OverallTable

class StatWindow(QWidget):
    """Окно статистики"""
    
    def __init__(self, ):
        QWidget.__init__(self)
        self.createLayout()
        self.createTimer()
        self.update()
        
    def createLayout(self):
        vlayout = QVBoxLayout()
        
        # Верхняя часть
        gl = QGridLayout()
        self.__batches_count_lbl = QLabel()
        self.__sheets_count_lbl = QLabel()
        self.__first_checkup_lbl = QLabel()
        self.__second_checkup_lbl = QLabel()
        self.__overall_pb = TwoProgressBar()
        gl.addWidget(self.__batches_count_lbl, 0, 0)
        gl.addWidget(self.__sheets_count_lbl, 1, 0)
        gl.addWidget(self.__first_checkup_lbl, 0, 1)
        gl.addWidget(self.__second_checkup_lbl, 0, 2)
        gl.addWidget(self.__overall_pb, 1, 1, 1, 2)
        gl.setColumnStretch(0, 0)
        gl.setColumnStretch(1, 1)
        gl.setColumnStretch(2, 1)
        
        vlayout.addLayout(gl)
        
        # Средняя часть
        hlayout = QHBoxLayout()
        self.__psws = []
        for g in Group.all():
            psw = ProblemStatWidget(g)
            self.__psws.append(psw)
            hlayout.addWidget(psw)
        
        vlayout.addLayout(hlayout)
        
        # Нижняя часть
        self.__overall_table = OverallTable(len(Batch.all()), 6)
        vlayout.addWidget(self.__overall_table)
        
        self.setLayout(vlayout)
        
    def createTimer(self):
        self.__update_timer = QTimer(self)
        self.__update_timer.setInterval(1000) # Обновляемся раз в секунду
        self.__update_timer.start()
        self.connect(self.__update_timer, SIGNAL("timeout()"), self.update)
        
    def update(self):
        QWidget.update(self)
        # Верхняя часть
        self.__batches_count_lbl.setText(QString(u"Пачек\n%d" % len(Batch.all())))
        self.__sheets_count_lbl.setText(QString(u"Работ\n%d" % Batch.total_papers))
        if chekup.checkup() >= 0:
            self.__first_checkup_lbl.setText(QString(u"Старт I проверки: " + chekup.startTimes()[0].toString("HH:mm")))
        if chekup.checkup() >= 1:
            self.__second_checkup_lbl.setText(QString(u"Старт II проверки: " + chekup.startTimes()[1].toString("HH:mm")))
        total_problems = float(len(Batch.all())*6)
        if total_problems > 0:
            self.__overall_pb.setValues((100.0*float(Batch.totalCopleteProblems(1))/total_problems, 100.0*float(Batch.totalCopleteProblems(0))/total_problems))
        # Средняя часть
        for psw in self.__psws:
            psw.update()
        # Нижняя часть
        self.__overall_table.update()